﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using BusinessObjects;
using System.Linq;

namespace UI
{
    public partial class frmKetQuaHocTap_Import : FormBase
    {
        public frmKetQuaHocTap_Import()
        {
            InitializeComponent();
        }

        public int NamThucTapID { get; set; }
        private DataTable _dtLoad;

        private void frmKetQuaHocTap_Import_Load(object sender, EventArgs e)
        {
            try
            {
                _dtLoad = new DataTable();
                _dtLoad.Columns.Add("Success", typeof(bool));
                _dtLoad.Columns.Add("Note", typeof(string));
                _dtLoad.Columns.Add("MaSinhVien", typeof(string));
                _dtLoad.Columns.Add("Ho", typeof(string));
                _dtLoad.Columns.Add("Ten", typeof(string));
                _dtLoad.Columns.Add("TK10_1", typeof(string));

                btnExcel_ItemClick(null, null);
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnExcel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                Dictionary<string, int> dicSinhVien = new Dictionary<string, int>();
                var querySinhVien = Session.DefaultSession.Query<SinhVienTrongGiangVien>().Where(c => c.GiangVienTrongNam.NamThucTap.Oid == NamThucTapID).Select(c => new
                {
                    c.Oid,
                    c.SinhVien.MaSinhVien,
                });
                foreach (var item in querySinhVien)
                {
                    dicSinhVien.Add(item.MaSinhVien, item.Oid);
                }

                OpenFileDialog op = new OpenFileDialog();
                op.Filter = "Excel 2003|*.xls";
                if (op.ShowDialog() == DialogResult.OK)
                {
                    _dtLoad.Rows.Clear();
                    ExcelLibrary.SpreadSheet.Workbook book = ExcelLibrary.SpreadSheet.Workbook.Load(op.FileName);
                    if (book.Worksheets.Count > 0)
                    {
                        ExcelLibrary.SpreadSheet.Worksheet sheet = book.Worksheets[0];
                        for (int row = 1; ; row++)
                        {
                            string strMaSinhVien = string.Empty;
                            if (sheet.Cells[row, 1].Value != null && sheet.Cells[row, 1].Value.ToString() != string.Empty)
                                strMaSinhVien = sheet.Cells[row, 1].Value.ToString();
                            string strHo = string.Empty;
                            if (sheet.Cells[row, 2].Value != null && sheet.Cells[row, 2].Value.ToString() != string.Empty)
                                strHo = sheet.Cells[row, 2].Value.ToString();
                            string strTen = string.Empty;
                            if (sheet.Cells[row, 3].Value != null && sheet.Cells[row, 3].Value.ToString() != string.Empty)
                                strTen = sheet.Cells[row, 3].Value.ToString();
                            string strTK10_1 = string.Empty;
                            if (sheet.Cells[row, 10].Value != null && sheet.Cells[row, 10].Value.ToString() != string.Empty)
                                strTK10_1 = sheet.Cells[row, 10].Value.ToString();

                            if (strMaSinhVien == string.Empty)
                                break;
                            else
                            {
                                bool isMaSinhVien = true;
                                if (!dicSinhVien.ContainsKey(strMaSinhVien))
                                    isMaSinhVien = false;
                                bool isDiem = true;
                                try { double i = Convert.ToDouble(strTK10_1); }
                                catch { isDiem = false; }

                                if (isMaSinhVien && isDiem)
                                {
                                    DataRow dr = _dtLoad.NewRow();
                                    dr["Success"] = true;
                                    dr["Note"] = "Dòng " + (row + 1);
                                    dr["MaSinhVien"] = strMaSinhVien;
                                    dr["Ho"] = strHo;
                                    dr["Ten"] = strTen;
                                    dr["TK10_1"] = strTK10_1;
                                    _dtLoad.Rows.Add(dr);
                                }
                                else
                                {
                                    DataRow dr = _dtLoad.NewRow();
                                    dr["Success"] = false;
                                    if (!isMaSinhVien)
                                        dr["Note"] = "Dòng " + (row + 1) + ": Sinh viên không có trong năm hoặc không được giảng viên hướng dẫn";
                                    else if (!isDiem)
                                        dr["Note"] = "Dòng " + (row + 1) + ": Nhập sai điểm";
                                    dr["MaSinhVien"] = strMaSinhVien;
                                    dr["Ho"] = strHo;
                                    dr["Ten"] = strTen;
                                    dr["TK10_1"] = strTK10_1;
                                    _dtLoad.Rows.Add(dr);
                                }
                            }
                        }
                    }
                    gvSinhVien.DataSource = _dtLoad;
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                DataRow[] drs = _dtLoad.Select("Success=1");
                if (drs.Length > 0)
                {
                    if (CommonConfirm("Bạn muốn lưu dữ liệu này?", "LUU"))
                    {
                        Dictionary<string, int> dicSinhVien = new Dictionary<string, int>();
                        var querySinhVien = Session.DefaultSession.Query<SinhVienTrongGiangVien>().Where(c => c.GiangVienTrongNam.NamThucTap.Oid == NamThucTapID).Select(c => new
                        {
                            c.Oid,
                            c.SinhVien.MaSinhVien,
                        });
                        foreach (var item in querySinhVien)
                        {
                            dicSinhVien.Add(item.MaSinhVien, item.Oid);
                        }

                        using (NestedUnitOfWork nuow = Session.DefaultSession.BeginNestedUnitOfWork())
                        {
                            for (int i = 0; i < drs.Length; i++)
                            {
                                DataRow dr = drs[i];
                                string strMaSinhVien = dr["MaSinhVien"].ToString();
                                string strTK10_1 = dr["TK10_1"].ToString();
                                int sinhVienID = dicSinhVien[strMaSinhVien];

                                var obj = nuow.Query<SinhVienTrongGiangVien>().FirstOrDefault(c => c.Oid == sinhVienID);
                                if (obj != null)
                                {
                                    obj.Diem = Convert.ToDouble(strTK10_1);
                                }
                            }
                            nuow.CommitChanges();
                        }
                        CommonMessage("Cập nhật thành công");
                        this.DialogResult = DialogResult.OK;
                    }
                }
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }

        private void btnCancel_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex)
            {
                CommonError(ex);
            }
        }
    }
}